﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Heroius.Stopwatch
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();

            Core.Elapsed += Core_Elapsed;

            CurrentPeriod = "00:00:00:00";

            DataContext = this;
        }

        private void Core_Elapsed(object sender, ElapsedEventArgs e)
        {
            var now = DateTime.Now;
            var p = now - RunTime + Residue;
            CurrentPeriod = $"{p.Hours}:{p.Minutes}:{p.Seconds}:{p.Milliseconds}";
            RaisePropertyChangedEvent("CurrentPeriod");
        }

        public string CurrentPeriod { get; set; }
        public WatchState State { get { return _state; } set { _state = value; RaisePropertyChangedEvent("State"); } }
        WatchState _state = WatchState.Sleep;
        public ObservableCollection<string> Logs { get; set; } = new ObservableCollection<string>();
        public Timer Core { get; set; } = new Timer();
        public DateTime RunTime { get; set; }
        public TimeSpan Residue { get; set; } = new TimeSpan(0);

        public event PropertyChangedEventHandler PropertyChanged;
        void RaisePropertyChangedEvent(string PropertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }

        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            Residue = new TimeSpan(0);
            State = WatchState.Run;
            RunTime = DateTime.Now;
            Core.Start();
            Logs.Add($"start {RunTime.Hour}:{RunTime.Minute}:{RunTime.Second}:{RunTime.Millisecond}");
        }

        private void BtnPause_Click(object sender, RoutedEventArgs e)
        {
            State = WatchState.Pause;
            var now = DateTime.Now;
            Residue += (now - RunTime);
            RunTime = now;
            Core.Stop();
            Logs.Add($"pause {now.Hour}:{now.Minute}:{now.Second}:{now.Millisecond}");
        }

        private void BtnGoon_Click(object sender, RoutedEventArgs e)
        {
            State = WatchState.Run;
            var now = DateTime.Now;
            Core.Start();
            Logs.Add($"recover {now.Hour}:{now.Minute}:{now.Second}:{now.Millisecond}");
        }

        private void BtnFinish_Click(object sender, RoutedEventArgs e)
        {
            State = WatchState.Sleep;
            var now = DateTime.Now;
            Core.Stop();
            Logs.Add($"finish {now.Hour}:{now.Minute}:{now.Second}:{now.Millisecond}");
        }
        
    }

    public enum WatchState
    {
        Sleep,
        Run,
        Pause
    }
}
